var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPrice:'',
    current: '',
    couponType:'',
    restaurantId:'',
    name:'',
    price:'',
    startTime:'',
    endTime:'',
    listData:[],
    nav_status_list: [//nav数据
      { name: "全部", value: "" },
      { name: "未使用", value: "0" },
      { name: "已使用", value: "1" },  
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    if(options.couponType){
       that.data.couponType = options.couponType;
    }
    if(options.restaurantId){
       that.data.restaurantId = options.restaurantId;
    }
    var current = "";
    if (options.paypage){
      this.data.paypage = options.paypage
    }
    if (options.totalPrice) {
      that.data.totalPrice = options.totalPrice;
    }
    

    that.setData({
      current: current,
    });
    that.setData(that.data)
    that.getData(current);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  getData(current) {//获取列表数据
    // wx.showLoading({
    //   title: '加载中',
    // });
    var that = this;
    var userId = app.globalData.id || wx.getStorageSync("id");
    
    if (userId){
      wx.request({
        url: applet_park_api_url+'/api/coupons',
        method: 'get',
        data: {
          "sysUserId": userId,
          "status": current,  //null全部  0未使用 1已使用 2已失效
          "foreignType":that.data.couponType, //1餐厅  2会议
          "foreignId":that.data.restaurantId,  //0通用
        },
        success: function (res) {
          
          if (res.data.code == 0) {
            for(var i=0;i<res.data.data.length;i++){
              res.data.data[i].endTime = that.handDate(res.data.data[i].endTime)
              res.data.data[i].startTime = that.handDate(res.data.data[i].startTime)
            }
            
            if(res.data.data.length == 0 ){
              wx.showToast({
                title:'暂无数据',
                icon: 'none'
              });
            }
            that.setData({
              listData: res.data.data
            });
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            });
          }
          wx.hideLoading();

        },
        fail: function (res) {

          wx.showToast({
            title: '网络异常，请稍后重试',
          })

        }
           
      })
    }
      
   
  },
  getTerm(){
    var date = '2018-10-02';
    return date;
  },
  handDate(e){
    
    e = e.substring(0,10);
    return e;
  },
  usecoupon(e){
    
    var that = this;
    var  index =e.currentTarget.dataset.index;
    var  status = e.currentTarget.dataset.status;
    var  couponId = e.currentTarget.dataset.couponid;
    var  price = e.currentTarget.dataset.price;
    if (status != 2){
        wx.showToast({
          title: '优惠券不能使用',
          icon:'none'
        })
      return 
    }else{
      this.setData({
        index: index,
     })
     var pages = getCurrentPages();
     var prevPage = pages[pages.length - 2]; //上一个页面
     var totalPricecoupon = 1;
     if( Number.parseInt(prevPage.data.totalPrice) >= Number.parseInt(price) ){
        totalPricecoupon = Number.parseInt(prevPage.data.totalPrice) - Number.parseInt(price)
     }else{
        totalPricecoupon = 1;
     }

       prevPage.setData({
         couponPrice:price,
         couponId:couponId,
         totalPricecoupon:totalPricecoupon,
         totalPrice:0
       });
       
       wx.navigateBack({
         delta: 1
       })
    }

      
        
      
     
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 导航事件
  nav_event(e) {
    const that = this;
    
    that.setData({
      current: e.currentTarget.dataset.index,
    })
    
    that.getData(e.currentTarget.dataset.index);
    
  },
})